<template>
  <!-- 放置弹层组件 -->
  <el-dialog title="新增部门" :visible="showDialog" @close="handleCloseDialog" @open="openHandleCloseDialog">
    <el-form label-width="120px">
      <template>
        <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门编码" prop="code">
            <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门负责人" prop="manager">
            <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
              <el-option
                v-for="item in peoples"
                :key="item.id"
                :value="item.username"
                :label="item.username"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="部门介绍" prop="introduce">
            <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
          </el-form-item>
        </el-form>
      </template>

    </el-form>
    <template #footer>
      <el-button type="primary" size="small" @click.native="clickSubmit">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    curNode: {
      type: Object,
      default() {
        return {}
      }
    },
    allList: {
      type: Array,
      default() {
        return []
      }
    }
  },

  data() {
    const validateCode = (rule, value, callback) => {
      // 1. 拿到所有的原始节点数据  allList
      // 2. allList里面每一项的code码拿到组成一个新数组

      const codeList = this.allList.map(item => item.code)
      let list = []
      if (this.form.id) {
        list = codeList.filter(item => item !== this.cachecode)
      } else {
        list = codeList
      }

      if (list.includes(value)) {
        return callback(new Error('当前code码重复'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      peoples: [],
      cachecode: 0
    }
  },
  //   mounted() {
  //     this.hGetEmployeeSimple()
  //   },
  methods: {
    handleCloseDialog() {
      this.$emit('close-dialog')
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
    },
    async hGetEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.peoples = res.data
    },
    openHandleCloseDialog() {
      this.hGetEmployeeSimple()
    //   this.hGetDepartments()
    },
    clickSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            // 调用更新接口
            await updateDepartments(this.form)
          } else {
            // 调用新增接口
            await addDepartments({ ...this.form, pid: this.curNode.id })
          }

          this.$emit('close-dialog')
          this.$emit('update-depart')
        }
      })
    },
    // 编辑
    async hGetDepartments(id) {
      const res = await getDepartDetail(id)
      this.form = { ...res.data }
      this.cachecode = res.data.code
      console.log(this.form)
    }
  }
}
</script>
